<link rel="stylesheet" href="layui/css/layui.css">

<body class="layui-layout-body">

<div style="padding: 15px;">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">球员名称</label>
                <div class="layui-input-block">
                          <input id="ID" name="ID" type="hidden" value="">
                    <input id="Name" type="text" name="Name" lay-verify="required" autocomplete="off" placeholder="请输入"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                  <input id="ID" type="hidden" name="" value="">
                <label class="layui-form-label">球员号码</label>
                <div class="layui-input-inline">
                    <input id="Number" type="text" name="Number" lay-verify="required" autocomplete="off"
                           placeholder="请输入" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">身高</label>
                <div class="layui-input-block">
                    <input id="Height" type="text" name="Height" lay-verify="required" autocomplete="off"
                           placeholder="请输入" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">体重</label>
                <div class="layui-input-inline">
                    <input id="Weight" type="text" name="Weight" lay-verify="required" autocomplete="off"
                           placeholder="请输入" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">电子邮件</label>
                <div class="layui-input-block">
                    <input id="Email" type="text" name="Email" lay-verify="required" autocomplete="off"
                           placeholder="请输入" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">出生年月日</label>
                <div class="layui-input-inline">
                    <input id="Birth" type="text" name="Birth" class="layui-input" placeholder="yyyy-MM-dd HH:mm:ss">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">赛季</label>
            <div class="layui-input-block" style="width: 250px">
                <input id="SeasonID" type="hidden" name="" value="">
                <select id="SeasonID_s" lay-verify="required" name="SeasonID" lay-filter="myselect">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>

        <!--
                    <div class="layui-form-item">
                        <label class="layui-form-label">球队</label>
                        <div class="layui-input-block">
                            <input id="TeamID" type="hidden" name="" value="">
                            <select id="TeamID_s" lay-verify="required" name="TeamID" lay-filter="myselect">
                                <option value="">请选择</option>
                            </select>
                        </div>
                    </div>-->


        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">Logo</label>
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" id="test1">上传图片</button>
                    <input type="hidden" id="PhotoSrc" name="PhotoSrc" value=""/>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" width="100px" height="80px" id="demo1"/>
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" type="button" lay-submit="" lay-filter="add">立即修改</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </form>

</div>
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.js"></script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script src="layui/layui.js"></script>
<script>

    layui.use(['table', 'form', 'upload', 'laydate'], function () {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#Birth',
            type: 'datetime'
        });
        var uploadInst = upload.render({
            elem: '#test1',
            url: 'http://svcsa.org/uploadimage',
            acceptMime: 'image/*',
            multiple: true,
            before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            },
            done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功
                console.log(res);
                picture = res.data.savename;
                $("#PhotoSrc").attr("value", picture)
            },
            error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText1');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
        form.on('submit(add)', function (data) {
            var index = parent.layer.getFrameIndex(window.name);
            axios.post('http://svcsa.org/BBPlayer/' + data.field.ID, {
                Name: data.field.Name,
                Number: data.field.Number,
                Birth: data.field.Birth,
                Height: data.field.Height,
                Weight: data.field.Weight,
                PhotoSrc: $("#PhotoSrc").val(),
                PhotoSrc: data.field.PhotoSrc,
                Email: data.field.Email,
                SeasonID: data.field.SeasonID,
//                        TeamID: data.field.TeamID,

            })
                .then(function (response) {
                    layer.msg('修改成功', {
                        icon: 1,
                        time: 1500
                    }, function () {
                        parent.layer.close(index);
                    });
                })
                .catch(function (error) {
                    console.log(error);
                });
        });
        axios.get('http://svcsa.org/BBSeason', {}).then(function (response) {
            console.log(response);
            $.each(response.data.data, function (index, item) {
                $('#SeasonID_s').append('<option value="' + item.SeasonID + '">' + item.SeasonName + '</option>');
            });
            form.render();
            var num = $("#SeasonID").val();
            var numbers = $("#SeasonID_s").find("option"); //获取select下拉框的所有值
            console.log(numbers);
            for (var j = 1; j < numbers.length; j++) {
                console.log(numbers);
                if ($(numbers[j]).val() == num) {
                    console.log('ok');
                    $(numbers[j]).attr("selected", "selected");
                    $(numbers[j]).attr("class", "layui-this");
                    form.render();
                }
            }
        })

        axios.get('http://svcsa.org/BBTeam', {}).then(function (response) {
            console.log(response);
            $.each(response.data.data, function (index, item) {
                $('#TeamID_s').append('<option value="' + item.ID + '">' + item.Name + '</option>');
            });
            form.render();
            var num = $("#TeamID").val();
            var numbers = $("#TeamID_s").find("option"); //获取select下拉框的所有值
            console.log(numbers);
            for (var j = 1; j < numbers.length; j++) {
                console.log(numbers);
                if ($(numbers[j]).val() == num) {
                    console.log('ok');
                    $(numbers[j]).attr("selected", "selected");
                    $(numbers[j]).attr("class", "layui-this");
                    form.render();
                }
            }
        })
            .catch(function (error) {
                console.log(error);
            });
    });
</script>

</body>